package com.markyao.utils;

import java.io.IOException;
import java.net.InetAddress;

public class LocalUtils {
    public static void main(String[] args) {
        System.out.println(getCpucore());
        System.out.println(getMemory());
        System.out.println(isReachable());
    }
    public static int getCpucore(){
        return Runtime.getRuntime().availableProcessors();
    }

    public static String getMemory(){
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory(); // 总内存
        long freeMemory = runtime.freeMemory();   // 空闲内存
        long usedMemory = totalMemory - freeMemory; // 已使用内存


        return "总内存: " + totalMemory+"-空闲内存: " + freeMemory+"-已使用内存: " + usedMemory;
    }

    public static boolean isReachable(){
        String hostName = "www.baidu.com"; // 要检查的主机名

        try {
            InetAddress inetAddress = InetAddress.getByName(hostName);
            boolean isReachable = inetAddress.isReachable(5000); // 设置超时时间（5秒）

            if (isReachable) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
